import {
  Controller,
  Get,
  Post,
  Put,
  Body,
  Query,
  UseGuards,
  Headers,
  Delete,
  Param,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';

import { BaseResponse } from '@server/basic-dto';

import BlogService from './service';
import { CreateOrUpdateBlogDto, BlogDetailResponse, BlogListResponse, BlogHomeResponse } from './dto';

@ApiTags('blog')
@Controller('/blog')
export default class BlogController {
  constructor(private readonly service: BlogService) {}

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '创建一篇支持多语言的新博客',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @ApiBearerAuth()
  @Put('/create')
  async create(
    @Body() createBlog: CreateOrUpdateBlogDto,
    @Headers('authorization') token: string,
  ): Promise<BaseResponse> {
    return this.service.create(createBlog, token);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '更新某一篇博客',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @ApiParam({
    name: 'id',
    description: '博客id',
  })
  @ApiBearerAuth()
  @Post('/update/:id')
  async update(
    @Param('id') id: string,
    @Body() updateBlog: CreateOrUpdateBlogDto,
  ): Promise<BaseResponse> {
    return this.service.update(id, updateBlog);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '设置博客在官网的显示隐藏',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @ApiParam({
    name: 'id',
    description: '博客id',
  })
  @ApiQuery({
    name: 'state',
    description: '1: 已上架(官网可以看到), 2: 已隐藏',
  })
  @ApiBearerAuth()
  @Post('/toggle/:id')
  async toggle(
    @Param('id') id: string,
    @Query('state') state: string,
  ): Promise<BaseResponse> {
    return this.service.toggle(id, state);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '删除多篇博客',
  })
  @ApiQuery({
    name: 'ids',
    required: true,
    description: '博客ids, 多个用id组合用“,”分隔',
  })
  @ApiBearerAuth()
  @Delete('/delete')
  async delete(@Query('ids') ids: string): Promise<BaseResponse> {
    return this.service.delete(ids);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '博客分页',
  })
  @ApiResponse({
    type: BlogListResponse
  })
  @ApiQuery({
    name: 'page',
    description: '当前页',
    required: false,
  })
  @ApiQuery({
    name: 'pageSize',
    description: '每页多少条',
    required: false,
  })
  @ApiQuery({
    name: 'keyword',
    description: '标题模糊搜索',
    required: false,
  })
  @ApiQuery({
    name: 'locale',
    description: '语言包(字典id)',
    required: false,
  })
  @ApiBearerAuth()
  @Get('/list')
  async list(
    @Query('page') page: number,
    @Query('pageSize') pageSize: number,
    @Query('keyword') keyword: string,
    @Query('locale') locale: string,
  ): Promise<BlogListResponse> {
    return this.service.list(page, pageSize, keyword, locale);
  }

  @ApiOperation({
    summary: '博客详情',
  })
  @ApiResponse({
    type: BlogDetailResponse
  })
  @ApiParam({
    name: 'id',
    description: '博客id',
  })
  @ApiBearerAuth()
  @Get('/detail/:id')
  async detail(@Param('id') id: string): Promise<BlogDetailResponse> {
    return this.service.detail(id);
  }

  @ApiOperation({
    summary: '博客分页(官网页面)',
  })
  @ApiResponse({
    type: BlogListResponse
  })
  @ApiQuery({
    name: 'page',
    description: '当前页',
    required: false,
  })
  @ApiQuery({
    name: 'pageSize',
    description: '每页多少条',
    required: false,
  })
  @ApiQuery({
    name: 'keyword',
    description: '标题模糊搜索',
    required: false,
  })
  @ApiQuery({
    name: 'locale',
    description: '语言包(字典id)',
    required: true,
  })
  @Get('/front-list')
  async frontlist(
    @Query('page') page: number,
    @Query('pageSize') pageSize: number,
    @Query('keyword') keyword: string,
    @Query('locale') locale: string,
  ): Promise<BlogListResponse> {
    return this.service.frontlist(page, pageSize, keyword, locale);
  }

  @ApiOperation({
    summary: '博客(官网首页)',
  })
  @ApiResponse({
    type: BlogHomeResponse
  })
  @ApiQuery({
    name: 'locale',
    description: '语言包(字典id)',
    required: true,
  })
  @Get('/front-home')
  async homelist(@Query('locale') locale: string,): Promise<BlogHomeResponse> {
    return this.service.homelist(locale);
  }

  @ApiOperation({
    summary: '博客详情(官网页面)',
  })
  @ApiResponse({
    type: BlogDetailResponse
  })
  @ApiParam({
    name: 'id',
    description: '博客id',
  })
  @Get('/front-detail/:id')
  async frontdetail(@Param('id') id: string): Promise<BlogDetailResponse> {
    return this.service.detail(id, true);
  }
}
